import BaseController from '#common/base_controller'
import Dict from '#models/dict'
import { createDictValidator, updateDictValidator } from '#validators/dict'
import type { HttpContext } from '@adonisjs/core/http'
import vine from '@vinejs/vine'

export default class ServiceRightsController extends BaseController {
  async list(ctx: HttpContext) {
    return this.generatePageData(ctx, Dict)
  }

  async create({ request }: HttpContext) {
    const data = await request.validateUsing(createDictValidator)
    return Dict.create(data)
  }

  async detail({ request }: HttpContext) {
    const { id } = request.qs()
    return Dict.findOrFail(id)
  }

  async update({ request }: HttpContext) {
    const id = request.param('id')
    const updateDate = await request.validateUsing(updateDictValidator)
    return Dict.query().where('id', id).update(updateDate)
  }

  async delete({ request }: HttpContext) {
    const idsValidator = vine.compile(
      vine.object({
        ids: vine.array(vine.number()),
      })
    )

    const data = await request.validateUsing(idsValidator)

    return Dict.query().whereIn('id', data.ids).delete()
  }
}
